import { action, observable } from 'mobx';
import { message } from 'antd';
import taskServer from '../services/task';
import projectServer from '../services/project';
import isSuccess from '../utils/error';
import Base from './base';
import { netCheckFn } from '../utils/netCheck';

const {
  getList,
  detail,
  complete,
  restart,
  createTask,
  saveinfo,
  repeatType,
  aheadType,
  saveRemind,
  addMember,
  deleteMember,
  setManager,
  setHost,
  alarmTaskList,
} = taskServer;

const { linkProjectList } = projectServer;

const membersApi = {
  addMember,
  deleteMember,
  setManager,
  setHost,
};

export default class TaskState extends Base {
  constructor(props) {
    super(props);
    // 注入其它需要调用的store
    setTimeout(() => {
      const { contactsState } = props;
      this.contactsState = contactsState;
    });
  }

  @observable taskList = [];
  @observable taskList0 = []; // 未完成任务列表
  @observable taskList1 = []; // 已完成任务列表
  @observable detailData = undefined;
  @observable detailDataForSide = undefined; // 侧边栏显示的detalData，避免于detailData冲突

  @observable linkProjectList = [];
  @observable alarmTaskList = [];
  @observable repeatList = [];
  @observable aheadList = [];
  @observable loaded = {
    project: false,
    task: false,
    detailSelect: false,
  };

  @action.bound
  async clearDetailData() {
    this.detailData = undefined;
  }
  @action.bound
  async getList(params, callback) {
    let res = await netCheckFn('taskList',async ()=>{
        const res = await getList(params);
        return res;
    });
    if (isSuccess(res)) {
      const list = res.data && Array.isArray(res.data) ? res.data : [];
      callback && callback(list);
      this.taskList = list;
      if (list.length) {
        const taskList0 = [];
        const taskList1 = [];
        list.forEach((l) => {
          l.taskstatus === 2 ? taskList1.push(l) : taskList0.push(l);
        });
        this.taskList0 = taskList0;
        // 组内排序由完成时间大到小排序
        let sortTaskList1 = taskList1;
        if (taskList1.length && taskList1.length > 1) {
          sortTaskList1 = taskList1.concat().sort((a, b) => {
            return Date.parse(new Date(b.completedtime)) - Date.parse(new Date(a.completedtime));
          });
        }
        this.taskList1 = sortTaskList1;
      }
    }
  }

  @action.bound
  async getDetailSelectList() {
    this.repeatList = [];
    this.aheadList = [];
    this.loaded.detailSelect = false;
    // 获取该任务重复频度类型
    // const repeatList = await repeatType();
    let repeatList = await netCheckFn('taskRepeatType',async ()=>{
        const res = await repeatType();
        return res;
    });
    this.repeatList = repeatList.data;
    // 获取该任务提前时间类型
    // const aheadList = await aheadType();
    let aheadList = await netCheckFn('taskAheadType',async ()=>{
        const res = await aheadType();
        return res;
    });
    this.aheadList = aheadList.data;
    this.loaded.detailSelect = true;
  }

  @action.bound
  async getLinkProjectList(searchkey = '') {
    this.linkProjectList = [];
    // 获取该任务可绑定的项目列表
    this.loaded.project = false;
    // const res = await linkProjectList({ searchkey });
    let res = await netCheckFn('taskLinkProjectList-' + searchkey,async ()=>{
        const res = await linkProjectList({ searchkey });
        return res;
    });
    if (isSuccess(res)) {
      // console.log('---getLinkProjectList---', res);
      this.linkProjectList = res.data;
    }
    this.loaded.project = true;
  }

  @action.bound
  async getAlarmTaskList() {
    this.alarmTaskList = [];
    this.loaded.task = false;
    // 获取任务提醒列表
    // const res = await alarmTaskList();
    let res = await netCheckFn('taskAlarmTaskList',async ()=>{
        const res = await alarmTaskList();
        return res;
    });
    if (isSuccess(res)) {
      // console.log('---getAlarmTaskList---', res);
      this.alarmTaskList = res.data;
    }
    this.loaded.task = true;
  }

  @action
  async getDetail({ taskid }, reflesh) {
    // const res = await detail({ taskid });
    let res = await netCheckFn('taskDetail-' + taskid,async ()=>{
        const res = await detail({ taskid });
        return res;
    });
    if (isSuccess(res)) {
      this.detailData = res.data;
    }
  }

  @action
  async clearDetail() {
    this.detailData = undefined;
  }

  @action
  async getDetailForSide({ taskid }) {
    // const res = await detail({ taskid });
    let res = await netCheckFn('taskDetail-' + taskid,async ()=>{
        const res = await detail({ taskid });
        return res;
    });
    if (isSuccess(res)) {
      this.detailDataForSide = res.data;
    }
  }

  @action.bound
  async complete(params, callback) {
    const res = await complete(params);
    if (isSuccess(res)) {
      callback && callback();
    }
  }

  @action.bound
  async restart(params, callback) {
    const res = await restart(params);
    if (isSuccess(res)) {
      callback && callback();
    }
  }

  @action.bound
  async createTask(params, callback) {
    const res = await createTask(params);
    if (isSuccess(res)) {
      // console.log('---createTask---', res);
      message[['error', 'success'][res.success]](res.message);
      callback && callback();
    }
  }

  @action.bound
  async saveinfo(params) {
    const res = await saveinfo(params);
    if (isSuccess(res)) {
      message[['error', 'success'][res.success]](res.message);
      if (!res.success) return;
      // 保持成功刷新列表和日期提醒数据
      this.getAlarmTaskList();
    }
  }

  @action
  async saveRemind(params, callback) {
    const res = await saveRemind(params);
    if (isSuccess(res)) {
      message[['error', 'success'][res.success]](res.message);
      console.log('---saveRemind---', res);
      callback && callback();
    }
  }

  // 任务成员请求接口的通用处理
  @action.bound
  async manageMember(key, params, callback) {
    const res = await membersApi[key](params);
    if (isSuccess(res)) {
      callback && callback();
      message[['error', 'success'][res.success]](res.message);
    }
  }
}
